<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>配置网关</title>
    </title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
    <style>
        .layui-form-item .layui-input-company {
            width: auto;
            padding-right: 10px;
            line-height: 38px;
        }

        .layui-input-block tip {
            font-size: 15px !important;
            margin-top: 16px !important;
        }

        #channel_rate {
            color: black;
        }

        ;
    </style>
</head>

<body>
    <div class="layuimini-container">
        <div class="layuimini-main">

            <div class="layui-form layuimini-form">
                <div class="layui-form-item">
                    <label class="layui-form-label required">网关编号</label>
                    <div class="layui-input-block">
                        <input type="text" name="Ct_candt" onkeyup="this.value=this.value.replace(/[^0-9]/g,'')"
                            lay-verify="required" lay-reqtext="当前网关编号不能为空" placeholder="请输入当前网关编号" value=""
                            class="layui-input">
                        <tip>当前局放终端数据上报间隔(秒)<span id="channel_rate" style="color: red;"></span></tip>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label required">局放终端数据上报间隔(秒)</label>
                    <div class="layui-input-block">
                        <input type="text" onkeyup="this.value=this.value.replace(/[^0-9]/g,'')" name="Ct_gateway"
                            lay-verify="required" lay-reqtext="局放终端数据上报间隔不能为空" placeholder="请配置网关通讯" value=""
                            class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" style="float: right;" lay-submit lay-filter="saveBtn">确认保存</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
    <script src="../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
    <script>
        layui.use(['form', 'miniTab'], function () {
            var form = layui.form,
                $ = layui.jquery,
                layer = layui.layer,
                miniTab = layui.miniTab;

            // 网关通道号
            var channelNo = null;

            var token = sessionStorage.getItem('newtoken');

            $("input[name = 'Ct_candt']").blur(function () {
                // $("input[name = 'Ct_candt']").css("background-color","#D6D6FF");

                var channelAddr = $("input[name ='Ct_candt']").val();

                if (channelAddr != '' && channelAddr != null) {
                    $.ajax({
                        url: '/oms-web/channels/' + channelAddr + '?token=' + token,
                        dataType: "json",
                        type: "get",
                        contentType: "application/json",
                        success: function (data) {
                            if (data.code == -1) {
                                window.location = './login-1.html'
                            }
                            if (data.code === 0) {
                                if (data.data.length == 0) {
                                    $('#channel_rate').text('暂无该网关！');
                                    $("input[name='Ct_gateway']").attr("readOnly", "true");
                                    $("input[name='Ct_gateway']").val("");
                                } else {
                                    $('#channel_rate').text(data.data[0].rate);
                                    $("input[name='Ct_gateway']").removeAttr("readOnly");
                                    channelNo = data.data[0].channelNo
                                }
                            } else {
                                layer.msg(data.msg, { icon: 1, time: 1000 });
                            }
                        },
                        error: function () {
                            layer.msg("数据请求错误！");
                        }
                    });
                } else {
                    layer.msg('当前网关编号不能为空');
                }

            });

            //监听提交
            form.on('submit(saveBtn)', function (data) {

                var str = data.field;

                // 截取 开关柜类型
                Ct_candt_type = str.Ct_candt.substring(2, 6);
                if (Ct_candt_type != '0001') {
                    layer.msg('该网关不属于开关柜类型！');
                    return;
                }

                layer.confirm('确定配置网关通讯频率吗？', {
                    btn: ['确定', '取消'] //按钮
                }, function () {
                    $.ajax({
                        url: '/oms-web/channels/' + str.Ct_candt + '/configure?token=' + token + '&channelCode=' + str.Ct_candt + '&channelNo=' + channelNo + '&rate=' + str.Ct_gateway,
                        dataType: "json",
                        type: "post",
                        contentType: "application/json",
                        success: function (data) {
                            if (data.code == -1) {
                                window.location = './login-1.html'
                            }
                            if (data.code === 0) {
                                layer.msg("修改成功！", { icon: 1, time: 1000 }, function () {
                                    window.location.reload();
                                })
                            } else {
                                layer.msg(data.msg, { icon: 1, time: 1000 });
                            }
                        },
                        error: function () {
                            layer.msg("数据请求错误！");
                        }
                    });
                });
            });

        });
    </script>
</body>

</html>